package com.zqw.service.impl;

import com.zqw.info.GoodsInfo;
import com.zqw.info.UserInfo;
import com.zqw.mapper.ShopCarMapper;
import com.zqw.mapper.UserMapper;
import com.zqw.pojo.GoodsPojo;
import com.zqw.pojo.UserPojo;
import com.zqw.service.UserService;
import com.zqw.ss.Transition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    ShopCarMapper shopCarMapper;

    //登录
    @Override
    public UserInfo login(UserPojo user) {
        UserInfo login = userMapper.login(user);
        return login;
    }

    @Override
    public boolean regist(UserInfo user) {
        return userMapper.regist(user);
    }

    @Override
    public Boolean soleName(String cname) {
        return userMapper.soleName(cname);
    }

    /**
     * 更新购物车
     * @param
     * @return
     */
    public List<GoodsInfo> updShopCar(List<GoodsInfo> goods, String uid){
        List<GoodsInfo> goodsInfos = new ArrayList<>();
        List<GoodsPojo> goodsPojos = shopCarMapper.queryShopCar(uid);
        Map<String, String> stringStringMap;
        boolean b;
        for (int i = 0; i < goodsPojos.size(); i++) {
            goodsInfos.add(Transition.transition(goodsPojos.get(i), GoodsInfo.class));
        }
        if(goods != null){
            List<GoodsInfo> goodsInfos2 = new ArrayList<>();
            boolean stat = true;
            for (int i = 0; i < goods.size(); i++) {
                stat = true;
                for (int j = 0; j < goodsInfos.size(); j++) {
                    if(goods.get(i).getGoodsId().equals(goodsInfos.get(j).getGoodsId())){
                        goodsInfos.get(j).setGoodsNumber(goodsInfos.get(j).getGoodsNumber()+goods.get(i).getGoodsNumber());
                        stringStringMap = new HashMap<>();
                        stringStringMap.put("cid",uid);
                        stringStringMap.put("gid",goods.get(i).getGoodsId());
                        stringStringMap.put("num",String.valueOf(goodsInfos.get(j).getGoodsNumber()));
                        stringStringMap.put("update_time",String.valueOf((new Date().getTime())/1000L));
                        b = shopCarMapper.updShopCar(stringStringMap);
                        if(b){
                            goodsInfos2.add(goods.get(i));
                        }else{
                            goodsInfos2.add(goodsInfos.get(i));
                        }
                        stat = false;
                        break;
                    }
                }
                if(stat){
                    stringStringMap = new HashMap<>();
                    stringStringMap.put("cid",uid);
                    stringStringMap.put("gid",goods.get(i).getGoodsId());
                    stringStringMap.put("num",String.valueOf(goods.get(i).getGoodsNumber()));
                    stringStringMap.put("create_time",String.valueOf((new Date().getTime())/1000L));
                    b= shopCarMapper.addShopCar(stringStringMap);
                    if(b){
                        goodsInfos2.add(goods.get(i));
                    }
                }
            }
            goodsInfos.addAll(goodsInfos2);
        }
        return goodsInfos;
    }

    /**
     * 添加购物项
     * @param goods
     * @param uid
     * @return
     */
    public boolean addShopCar(GoodsInfo goods,String uid){
        Map<String, String> map = new HashMap<>();
        map.put("cid",uid);
        map.put("gid",goods.getGoodsId());
        map.put("num",String.valueOf(goods.getGoodsNumber()));
        map.put("create_time",String.valueOf((new Date().getTime())/1000L));
        boolean b = shopCarMapper.addShopCar(map);
        return b;
    }

    /**
     * 删除购物项
     * @param goods
     * @param uid
     * @return
     */
    public boolean delShopCar(GoodsInfo goods,String uid){
        Map<String, String> map = new HashMap<>();
        map.put("cid",uid);
        map.put("gid",goods.getGoodsId());
        map.put("update_time",String.valueOf((new Date().getTime())/1000L));
        boolean b = shopCarMapper.delShopCar(map);
        return b;
    }

    /**
     * 删除购物项的个数
     * @param goods
     * @param uid
     * @return
     */
    public boolean updShopCarNum(GoodsInfo goods,String uid){
        Map<String, String> map = new HashMap<>();
        map.put("cid",uid);
        map.put("gid",goods.getGoodsId());
        map.put("num",String.valueOf(goods.getGoodsNumber()));
        map.put("update_time",String.valueOf((new Date().getTime())/1000L));
        boolean b = shopCarMapper.updShopCar(map);
        return b;
    }

}
